const { ipcMain, BrowserWindow, globalShortcut } = require('electron')
const path = require('path')


const loginWinFn = () => {
    ipcMain.on('event-win-showLogin', (event) => {
        const winchild = new BrowserWindow({
            width: 350,
            height: 530,
            show: false,
            frame: false,
            webPreferences: {
                preload: path.join(__dirname, '../preload/openLoginWin.ts')
            },
        })

        // 优雅打开窗口
        winchild.on('ready-to-show', () => winchild.show())

        // 打开窗口
        winchild.loadURL(path.join(__dirname, '../src/view/login/index.html'))

        // 关闭登录的子窗口
        closeLoginWin(winchild)

        // 接受授权成功后渲染进程传递的cookie
        ipcMain.on('event-win-sendCookie', (e, cookie) => {
            console.log(cookie);
            event.sender.send('event-win-senderCookie', cookie)
        })
        // 打开登录窗口的开发者工具
        globalShortcut.register('Ctrl+Shift+L', () => winchild.webContents.openDevTools())
    })
}

// 关闭登录的子窗口
const closeLoginWin = (winchild) => ipcMain.on('event-win-closeLoginWin', () => winchild.destroy())

module.exports = loginWinFn



